package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.Fhrb_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


public class FhrbController extends Controller {

    private final FormFactory formFactory;
    private final Fhrb_Repository fhrb_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public FhrbController(FormFactory formFactory, Fhrb_Repository fhrb_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.fhrb_Repository =  fhrb_Repository;
        this.ec = ec;
    }

    public CompletionStage<Result> detailList() {
        JsonNode json = request().body().asJson();
        String StartDate= json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String THDH = json.findPath("THDH").textValue();
        return fhrb_Repository.detailList(StartDate,EndDate,THDH).thenApplyAsync(List -> {
            return ok(Json.toJson(List));
        }, ec.current());
    }

}
